<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <div>
        <p>使用原生js，addEventListener，给每一li元素绑定一个click事件输出它们的顺序。</p>
    </div>
<ul>
    <li>a</li>
    <li>a</li>
    <li>a</li>
    <li>a</li>
</ul>
<script>
    //错误做法，存在闭包
    function test(){
        var lis = document.getElementsByTagName("li");
        for(var i=0; i<lis.length; i++){
            // lis[i].addEventListener("click",function () {
            //     console.log(i);
            // },false)
        }
    }
    test();

    //正确做法，使用立即执行函数处理闭包
    function write() {
        var lis = document.getElementsByTagName("li");
        for(var i=0; i<lis.length; i++){
            (function (j) {
                lis[j].addEventListener("click",function () {
                    console.log(j);
                },false)
            }(i))
        }
    }
    write();

    //第一种
    var str1 = "hacoas哈哈哈";
    function retBytesLen1(target) {
        var count = 0;
        for(var i=0; i<target.length; i++){
            if(target.charCodeAt(i) <= 255){
                count ++;
            }else if(target.charCodeAt(i) >255){
                count += 2;
            }
        }
        console.log(count);
    }
    retBytesLen1(str1);

    //第二种
    var str2 = "s哈哈哈";
    function retBytesLen2(target) {
        var count = target.length;
        for(var i=0; i<target.length; i++){
            if(target.charCodeAt(i) > 255){
                count ++;
            }
        }
        console.log(count);
    }
    retBytesLen2(str2);

    var f = (
        function f(){
            return "1";
        },
            function g(){
                return 2
            }
    )();
    typeof f;//Number
    console.log(typeof f);

    var x = 1;
    if(function j(){}){
        x += typeof j;
    }
    console.log(x);

</script>

</body>
</html>
